package cn.bdqn.sms.common.exception;

import cn.bdqn.sms.common.R.R;
import cn.bdqn.sms.common.R.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author zhangpengxiang
 * @description 全局异常处理器
 * @date 2024/11/19
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 处理登录异常
     */
    //   @ExceptionHandler(LoginException.class)
//    public R<?> handleLoginException(LoginException e) {
//        log.error(e.getMessage(), e);
//        return R.failure(e.getMessage());
//    }

    /**
     * 处理业务异常
     */
    //   @ExceptionHandler(BusinessException.class)
//    public R<?> handleBusinessException(BusinessException e, HttpServletRequest request) {
//        log.error(e.getMessage(), e);
//        Integer code = e.getCode();
//        if (code!= null) {
//            return R.failure(ResultCodeEnum.fromCode(code));
//        }
//        return R.failure(e.getMessage());
//    }

    @ExceptionHandler(BindException.class)
    public R handlerBindException(BindException e){
        log.error(e.getMessage(),e);
        return R.failure("数据校验失败！");
    }

    /**
     * 处理其他未捕获的通用异常
     */
    @ExceptionHandler(Exception.class)
    public R handleGeneralException(Exception e) {
        log.error(e.getMessage(), e);
        return R.failure(ResultCodeEnum.INTERNAL_SERVER_ERROR);
    }
}
